/*
*  文件名称：mainwindows.h
*  项目：超声波流量表无线抄表系统（上位机程序）
*  开发环境:Qt 5.14.2 +  MSVC2017
*  李志坚、张国栋、陈昊然、高华阳、唐海森、张暄、李铠铮
*  2024山东大学（威海）嵌入式开发实训小组
*  2024-10-18
*  开源地址：https://gitee.com/sduwh-embedded-training-group/wireless-ultrasonic-flowmeter-monitoring-system
*/

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QSplineSeries>
#include <QValueAxis>
//#include <QSerialPortInfo>
#include<QSerialPort>

QT_CHARTS_USE_NAMESPACE

#define MAX_X   75    //50
#define MAX_Y   105   //70
#define TEMPERATURE_TIME_MAX 20
#define TEMPERATURE_MIN        -10
#define TEMPERATURE_MAX        60

#define QUANTITY_TIME_MAX    20
#define QUANTITY_MIN        0
#define QUANTITY_MAX        60

#define TRUE    1
#define FALSE   0
#define  YES    1
#define  NO     0

# define SERIAL_BUAD_FRE  9600

//串口状态机的状态
#define  NO_STATE            0
#define  SEARCH_STATE        1
#define  HANDSHAKE_STATE     2
#define  MONITOR_STATE       3

//串口数据包
typedef struct _seriesDataPacket_{
    int number;        //设备编号
    float Current_T;   //温度
    float Current_Q;   //流量
}SeriesDataPacket_;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    QTimer *timer_DrawLine;
    QChart *chart;
    QSplineSeries *lineSeries;

    QChart *Q_chart;
    QSplineSeries *Q_lineSeries;

    //定义串口指针
    QSerialPort * serialPort;
    //串口监测的数据包
    SeriesDataPacket_  SeriesDataPacket;
    //串口数据处理状态机
    int SerialReadStateMachine = NO_STATE;
    //当前上位机连接的设备
    int current_device = 0;

private slots:
    void T_Chart_Init();
    void Q_Chart_Init();
    void DrawLine();
    void Series_Search_Init();
    void send_search_all_device_code();
    int analys_search_code_packet(const QByteArray &data);
    void onfindDeviceFinished();
    void ReadSerialData();
    void SendSerialData(const QByteArray &data);
    void on_searchPort_clicked();
    void on_openPort_clicked();
    int if_connect_device(const QByteArray &data);
    int decodeSeriesDataPacket(const QByteArray &data);



    void on_findDevice_clicked();

    void on_startView_clicked();

    void on_endView_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
